package com.cn.jdk;

import com.cn.service.AccountService;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;

public class JdkProxy implements InvocationHandler {
    private AccountService accountService;

    //生成代理对象
    public Object createProxy(AccountService accountService){
        this.accountService=accountService;
        ClassLoader classLoader=JdkProxy.class.getClassLoader();
        Class[] classes=accountService.getClass().getInterfaces();
        return Proxy.newProxyInstance(classLoader,classes,this);
    }
    @Override
    public Object invoke(Object proxy, Method method, Object[] objects) throws Throwable {
        MyAspect myAspect=new MyAspect();
        myAspect.beforeLog();
        Object invoke = method.invoke(accountService, objects);
        myAspect.afterLog();

        return proxy;
    }
}
